/*
package com.primaryedu.loadimage;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.primaryedu.R;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

*/
/**
 * �ڴ滺���ļ������첽��������ͼƬ
 *
 * @author ninglv
 *//*


public class LoadImage {

    private ExecutorService executorService;
    private ImageMemoryCache memoryCache;// �ڴ滺��
    private ImageFileCache fileCache;// ͼƬ����
    private Map<String, ImageView> taskMap;// ���ڴ������
    private static LoadImage instance;

    private Context mContext;

    private LoadImage() {
        // ��ʼ���̳߳���5���߳�
        executorService = Executors.newFixedThreadPool(3);

        memoryCache = new ImageMemoryCache();
        fileCache = new ImageFileCache();
        taskMap = new HashMap<String, ImageView>();
    }

    // ����ģʽ
    public static LoadImage getInstance() {
        if (instance == null) {
            instance = new LoadImage();
        }
        return instance;
    }

    public void setContext(Context context) {
        mContext = context;
    }

    // �������
    public void addTask(String url, ImageView img) {
        // ���ȴ��ڴ���ȡ��ͼƬ���Ƿ�Ϊ��
        Bitmap bitmap = memoryCache.getBitmapFromCache(url);

        boolean isAutoSize = false;
        if (img.getTag(R.id.load_image_is_auto_size) != null) {
            isAutoSize = (Boolean) img.getTag(R.id.load_image_is_auto_size);
        }

        if (bitmap != null) {
            if (isAutoSize) {
                resizeImageView(bitmap, img);
            }
            img.setImageBitmap(bitmap);
        } else {
            synchronized (taskMap) {
                taskMap.put(Integer.toString(img.hashCode()), img);
            }
        }
    }

    // ִ������
    public void doTask() {
        synchronized (taskMap) {
            Collection<ImageView> con = taskMap.values();
            for (ImageView i : con) {
                if (i != null) {
                    // �˴���tagΪImageView���õ�ͼƬURL
                    if (i.getTag() != null) {
                        loadImage((String) i.getTag(), i);
                    }
                }
            }
            // ִ�к��������������
            taskMap.clear();
        }
    }

    // ׼�����񣬷�ֹǰ���ſ���ͼƬ����ʾ
    public void prepare(int firstVisibleItem, int visibleItemCount) {
        if (firstVisibleItem >= visibleItemCount)
            return;
        int count = 0;
        synchronized (taskMap) {
            Collection<ImageView> con = taskMap.values();
            for (ImageView i : con) {
                if (i != null && count < visibleItemCount) {
                    // �˴���tagΪImageView���õ�ͼƬURL
                    if (i.getTag() != null) {
                        loadImage((String) i.getTag(), i);
                    }
                }
                count++;
            }
            // ִ�к��������������
            taskMap.clear();
        }
    }

    // ����ͼƬ
    private void loadImage(String url, ImageView img) {
        // �����µ�����
        executorService.submit(new TaskWithResult(new TaskHandler(url, img),
                url));
    }

    */
/**
     * ���һ��ͼƬ,�������ڴ滺��,Ȼ�����ļ�����,���������ȡ
     *
     * @param url
     * @return
     *//*

    public Bitmap getBitmap(String url) {
        // ���ڴ滺���л�ȡͼƬ
        Bitmap result;
        result = memoryCache.getBitmapFromCache(url);

        if (result == null) {
            // �ļ������л�ȡ
            result = fileCache.getImage(url);

            if (result == null) {
                // �������ȡ
                result = ImageGetForHttp.downloadBitmap(url);

                if (result != null) {
                    Log.e("@@@@@@", "�������ȡ");
                    // �������ȡ�ɹ���������ڴ滺��
                    memoryCache.addBitmapToCache(url, result);
                    // ��ͼƬ���浽SD��
                    fileCache.saveBmpToSd(result, url);
                }
            } else {
                Log.e("@@@@@@", "���ļ���ȡ");
                // ���ļ��ж�ȡ�ɹ�����ӵ��ڴ滺��
                memoryCache.addBitmapToCache(url, result);
            }
        } else {
            Log.e("@@@@@@", "���ڴ��ȡ");
        }
        return result;
    }

    // ������Ϣ
    private class TaskHandler extends Handler {

        String url;
        ImageView img;

        public TaskHandler(String url, ImageView img) {
            this.url = url;
            this.img = img;
        }

        @Override
        public void handleMessage(Message msg) {

            // �鿴imageview��Ҫ��ʾ��ͼƬ�Ƿ񱻸ı�
            if (img.getTag().equals(url)) {
                if (msg.obj != null) {
                    Bitmap bitmap = (Bitmap) msg.obj;

                    boolean isAutoSize = false;
                    if (img.getTag(R.id.load_image_is_auto_size) != null) {
                        isAutoSize = (Boolean) img
                                .getTag(R.id.load_image_is_auto_size);
                    }

                    if (isAutoSize) {
                        resizeImageView(bitmap, img);
                    }
                    img.setImageBitmap(bitmap);
                    // ���ý��䶯��
                    Animation animation = new AlphaAnimation(0, 1);
                    // ���ö���ʱ��Ϊ1��
                    animation.setDuration(1000);
                    img.startAnimation(animation);
                }
            }
        }
    }

    // ���߳�����
    private class TaskWithResult implements Callable<String> {

        private String url;
        private Handler handler;

        public TaskWithResult(Handler handler, String url) {
            this.url = url;
            this.handler = handler;
        }

        public String call() throws Exception {

            Message msg = new Message();
            msg.obj = getBitmap(url);

            if (msg.obj != null) {
                handler.sendMessage(msg);
            }
            return url;
        }
    }

    */
/**
     * �Զ�����
     *
     * @param bitmap
     * @param imageView
     * @author Stone
     *//*

    private void resizeImageView(final Bitmap bitmap, ImageView imageView) {

        if (mContext == null) {
            Log.e("@@@@@@", "LoadImage.resizeImageView===����������������");
            return;
        }

        int width = bitmap.getWidth();
        WindowManager wm = (WindowManager) mContext
                .getSystemService(Context.WINDOW_SERVICE);
        int screenWidth = wm.getDefaultDisplay().getWidth();

        imageView.setScaleType(ScaleType.FIT_XY);
        double ratio = screenWidth * 1.0 / width;
        LayoutParams lp = imageView.getLayoutParams();
        lp.width = screenWidth;
        lp.height = (int) (bitmap.getHeight() * ratio);
        imageView.setLayoutParams(lp);
    }
}
*/
